﻿/**
 * 倒计时插件
 */
(function ($) {
    $.fn.yomi = function () {
        var emsg = '';
        var etime = "";
        var _DOM = null;
        var TIMER;
        createdom = function (dom) {
            _DOM = dom;
            etime = $(dom).data("etime");
            emsg = $(dom).data("emsg");
            $(_DOM).append("<div class='yomi'><span class='yomiday'></span><span class='split'>天</span><span class='yomihour'></span><span class='split'>时</span><span class='yomimin'></span><span class='split'>分</span><span class='yomisec'></span><span class='split'>秒</span></div>")
            reflash();
        };
        reflash = function () {
            var nowtime = (new Date()).getTime() / 1000;
            if (nowtime > etime) { //已结束
                clearInterval(TIMER);
                $(_DOM).text(emsg);
                return;
            }
            var range = etime - Math.round((new Date()).getTime() / 1000),
                secday = 86400, sechour = 3600,
                days = parseInt(range / secday),
                hours = parseInt((range % secday) / sechour),
                min = parseInt(((range % secday) % sechour) / 60),
                sec = ((range % secday) % sechour) % 60;
            $(_DOM).find(".yomiday").html(nol(days));
            $(_DOM).find(".yomihour").html(nol(hours));
            $(_DOM).find(".yomimin").html(nol(min));
            $(_DOM).find(".yomisec").html(nol(sec));
        };
        TIMER = setInterval(reflash, 1000);
        nol = function (h) {
            return h > 9 ? h : '0' + h;
        }
        return this.each(function () {
            var $box = $(this);
            createdom($box);
        });
    }
})(jQuery);
$(function () {
    $(".yomibox").each(function () {
        $(this).yomi();
    });
});